﻿Imports DevExpress.XtraEditors

Public Class frmDuToan_CapNhat
    Dim Me_AddEdit As String
    Dim Me_Dtb_SoTc As New DataTable
    Dim Me_Dtb As New DataTable

#Region "Proc"

    Private Sub InitControl()
        Me.icbMaLoaiDt.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.txtSoDt.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.datNgayLap.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.lueSoTc.Properties.AppearanceFocused.BackColor = Project_FocusedColor
        Me.lueMaDv.Properties.AppearanceFocused.BackColor = Project_FocusedColor

    End Sub

    Private Sub InitLookup_DonVi()
        Dim myDtb As New DataTable

        Me.lueMaDv.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("madv", "Mã đơn vị", 30, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("tendv", "Tên đơn vị", 70, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Default), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("donvi", "", 85, DevExpress.Utils.FormatType.None, "", False, DevExpress.Utils.HorzAlignment.Default)})
        Me.lueMaDv.Properties.DisplayMember = "donvi"
        Me.lueMaDv.Properties.ValueMember = "madv"

        Dim myDvF As New DonViF
        myDtb = myDvF.GetAll
        Me.lueMaDv.Properties.DataSource = myDtb

    End Sub

    Private Sub InitLookup_ThiCong()
        Me.lueSoTc.Properties.Columns.AddRange(New DevExpress.XtraEditors.Controls.LookUpColumnInfo() {New DevExpress.XtraEditors.Controls.LookUpColumnInfo("sotc", "Số hồ sơ", 30, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("_ngaylap", "Ngày lập", 40, DevExpress.Utils.FormatType.None, "", True, DevExpress.Utils.HorzAlignment.Center), New DevExpress.XtraEditors.Controls.LookUpColumnInfo("_sotc", "", 30, DevExpress.Utils.FormatType.None, "", False, DevExpress.Utils.HorzAlignment.Default)})
        Me.lueSoTc.Properties.DisplayMember = "_sotc"
        Me.lueSoTc.Properties.ValueMember = "sotc"
    End Sub

    Private Function GetNewSoDt() As String
        Dim s As String
        Dim myDtF As New DuToanF
        Dim myDtb As New DataTable
        myDtb = myDtF.GetLastSoDt
        If myDtb.Rows.Count = 1 Then
            s = (CLng(myDtb.Rows(0).Item("sodt").ToString.Substring(2, 4)) + 1).ToString
            Dim i As Integer
            For i = 1 To 4 - s.Length
                s = "0" + s
            Next
            Return s
        Else
            Return "0001"
        End If

    End Function

    Private Sub AddNew()
        Me.icbMaLoaiDt.EditValue = ""
        Me.txtSoDt.EditValue = Project_Nam.ToString.Substring(2, 2) + GetNewSoDt()
        Me.datNgayLap.EditValue = Now.Date
        Me.lueMaDv.EditValue = ""
        Me.lueSoTc.EditValue = ""
        Me_AddEdit = "add"
        Status(1)

        LoadGrid_SoTc("000000", "D")
    End Sub

    Private Sub LoadEdit()
        Dim myDtF As New DuToanF
        Dim Dtb As New DataTable
        Dtb = myDtF.GetWhereSoDt(frmDuToan_CapNhat_SoDt)
        If Dtb.Rows.Count = 1 Then
            Me.icbMaLoaiDt.EditValue = Dtb.Rows(0).Item("maloaidt")
            Me.txtSoDt.EditValue = Dtb.Rows(0).Item("sodt")
            Me.datNgayLap.EditValue = Dtb.Rows(0).Item("ngaylap")
            Me.lueMaDv.EditValue = Dtb.Rows(0).Item("madv")
            Me.lueSoTc.EditValue = Dtb.Rows(0).Item("sotc")
        End If

        LoadGrid_SoDt(Me.txtSoDt.EditValue.ToString.Trim)
        Status(0)
        Me_AddEdit = "edit"
    End Sub

    Private Sub InitComboBox_LoaiDt()
        Me.icbMaLoaiDt.Properties.Items.AddRange(New DevExpress.XtraEditors.Controls.ImageComboBoxItem() {New DevExpress.XtraEditors.Controls.ImageComboBoxItem("D  Vật tư điện lực", "D", -1)})
        Me.icbMaLoaiDt.Properties.Items.AddRange(New DevExpress.XtraEditors.Controls.ImageComboBoxItem() {New DevExpress.XtraEditors.Controls.ImageComboBoxItem("K  Vật tư khách hàng", "K", -1)})
        Me.icbMaLoaiDt.Properties.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Default
        Me.icbMaLoaiDt.EditValue = ""
    End Sub

    Private Sub InitGrid()
        Dim i As Integer
        With Me.GridView1
            i = 0
            .Columns(i).FieldName = "ma"
            .Columns(i).Visible = False

            i = i + 1
            .Columns(i).FieldName = "ten"
            .Columns(i).Caption = "Tên vật tư"
            .Columns(i).Width = 100

            i = i + 1
            .Columns(i).FieldName = "dvt"
            .Columns(i).Caption = "Đơn vị tính"
            .Columns(i).Width = 30

            i = i + 1
            .Columns(i).FieldName = "sl"
            .Columns(i).Caption = "Số lượng"
            .Columns(i).Width = 40

            For i = 0 To .Columns.Count - 1
                .Columns(i).OptionsColumn.AllowEdit = False
            Next

        End With

    End Sub

    Private Sub LoadGrid_SoDt(ByVal SoDt As String)
        Dim myDuToanCtF As New DuToanCtF
        Me_Dtb = myDuToanCtF.GetWhereSoDt(SoDt)
        Me.GridControl1.DataSource = Me_Dtb

    End Sub

    Private Sub LoadGrid_SoTc(ByVal SoTc As String, ByVal MaLoaiDt As String)
        Dim myDuToanCtF As New DuToanCtF
        Me_Dtb = myDuToanCtF.GetWhereSoTc(SoTc, MaLoaiDt)
        Me.GridControl1.DataSource = Me_Dtb

    End Sub

    Private Function CheckInput() As Boolean
        If Me.icbMaLoaiDt.EditValue <> "D" And Me.icbMaLoaiDt.EditValue <> "K" Then
            XtraMessageBox.Show("Loại dự toán không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Me.icbMaLoaiDt.Focus()
            Return False
            Exit Function
        End If

        If Me.txtSoDt.EditValue.ToString.Length <> 6 Or Not IsNumeric(Me.txtSoDt.EditValue.ToString.Trim) Then
            XtraMessageBox.Show("Số dự toán không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Me.txtSoDt.Focus()
            Return False
            Exit Function
        End If

        If Me.datNgayLap.DateTime.Month <> Project_Thang Or Me.datNgayLap.DateTime.Year <> Project_Nam Then
            XtraMessageBox.Show("Ngày lập phải nằm trong tháng năm làm việc", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.datNgayLap.Focus()
            Return False
            Exit Function
        End If

        If Me.lueMaDv.EditValue = "" Then
            XtraMessageBox.Show("Đơn vị không hợp lệ", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
            Me.lueMaDv.Focus()
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Sub Status(ByVal i As Integer)
        Select Case i
            Case 0 'xem
                Me.icbMaLoaiDt.Enabled = False
                Me.txtSoDt.Enabled = False
                Me.datNgayLap.Enabled = False
                Me.lueSoTc.Enabled = False
                Me.lueMaDv.Enabled = False

                Me.btnThemDt.Text = "Thêm dự toán"
                Me.btnSuaDt.Text = "Sửa"
                Me.btnSuaDt.Enabled = True
                Me.btnXoaDt.Enabled = True
                Me.btnXem.Enabled = True

                Me.cmdThemVatTu.Enabled = False
                Me.cmdXoaVatTu.Enabled = False

                Me.GridView1.Columns(3).OptionsColumn.AllowEdit = False
            Case 1 'them
                Me.icbMaLoaiDt.Enabled = True
                Me.txtSoDt.Enabled = True
                Me.datNgayLap.Enabled = True
                Me.lueSoTc.Enabled = True
                Me.lueMaDv.Enabled = True

                Me.btnThemDt.Text = "Ghi dự toán"
                Me.btnSuaDt.Text = "Bỏ qua"
                Me.btnSuaDt.Enabled = True
                Me.btnXoaDt.Enabled = False
                Me.btnXem.Enabled = False

                Me.cmdThemVatTu.Enabled = True
                Me.cmdXoaVatTu.Enabled = True

                Me.GridView1.Columns(3).OptionsColumn.AllowEdit = True

            Case 2 'sua
                Me.icbMaLoaiDt.Enabled = True
                Me.txtSoDt.Enabled = False
                Me.datNgayLap.Enabled = True
                Me.lueSoTc.Enabled = True
                Me.lueMaDv.Enabled = True

                Me.btnThemDt.Text = "Ghi dự toán"
                Me.btnSuaDt.Text = "Bỏ qua"
                Me.btnSuaDt.Enabled = True
                Me.btnXoaDt.Enabled = False
                Me.btnXem.Enabled = False

                Me.cmdThemVatTu.Enabled = True
                Me.cmdXoaVatTu.Enabled = True

                Me.GridView1.Columns(3).OptionsColumn.AllowEdit = True

        End Select
    End Sub

    Private Function CheckKey() As Boolean
        Dim myDuToanF As New DuToanF
        If myDuToanF.ExistsSoDt(Me.txtSoDt.EditValue.ToString.Trim) Then
            XtraMessageBox.Show("Số dự toán đã tồn tại", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Me.txtSoDt.Focus()
            Return False
            Exit Function
        End If
        Return True
    End Function

    Private Sub AddDetail()
        'dua danh sach ma trong grid vao _dtb
        frmDuToan_CapNhat_Dtb = New System.Data.DataTable("MyDTB")
        frmDuToan_CapNhat_Dtb.Columns.Add("ma", GetType(String))
        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            frmDuToan_CapNhat_Dtb.Rows.Add(New Object() {Me.GridView1.GetDataRow(i).Item("ma")})
        Next

        'mo form tra cuu
        Project_Form = Me.Name
        Dim f As New frmDanhMuc_TraCuu
        f.ShowDialog()

        'them vat tu
        Dim myDanhMucF As New DanhMucF
        Dim myDtb As New DataTable
        For i = 0 To frmDanhMuc_TraCuu_Dtb.Rows.Count - 1
            myDtb = myDanhMucF.GetWhereMa(frmDanhMuc_TraCuu_Dtb.Rows(i).Item("ma"))
            If myDtb.Rows.Count = 0 Then Exit Sub

            Me_Dtb.Rows.Add()
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ma") = myDtb.Rows(0).Item("ma")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("ten") = myDtb.Rows(0).Item("ten")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("dvt") = myDtb.Rows(0).Item("dvt")
            Me_Dtb.Rows(Me_Dtb.Rows.Count - 1).Item("sl") = 0
        Next

    End Sub

    Private Function SaveMaster() As Boolean
        Dim myDt As New DuToan
        Dim myDtF As New DuToanF

        myDt.MaLoaiDt = Me.icbMaLoaiDt.EditValue
        myDt.SoDt = Me.txtSoDt.EditValue.ToString.Trim
        myDt.NgayLap = Me.datNgayLap.EditValue
        myDt.MaDv = Me.lueMaDv.EditValue
        myDt.SoTc = Me.lueSoTc.EditValue

        If Not myDtF.Save(myDt) Then
            XtraMessageBox.Show("Ghi tiêu đề dự toán không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
            Exit Function
        End If

        Return True
    End Function

    Private Function SaveDetail() As Boolean
        Dim myDtF As New DuToanF
        Dim myDtCt As New DuToanCt
        Dim myDtCtF As New DuToanCtF

        If Not myDtCtF.Delete(Me.txtSoDt.EditValue.ToString.Trim) Then
            Exit Function
            Return False
        End If

        Dim i As Integer
        For i = 0 To Me.GridView1.DataRowCount - 1
            If Me.GridView1.GetDataRow(i).Item("sl") <> 0 Then
                myDtCt.SoDt = Me.txtSoDt.EditValue.ToString.Trim
                myDtCt.Ma = Me.GridView1.GetDataRow(i).Item("ma")
                myDtCt.Sl = Me.GridView1.GetDataRow(i).Item("sl")

                If Not myDtCtF.Insert(myDtCt) Then
                    XtraMessageBox.Show("Ghi chi tiết vật tư lỗi tại dòng " + i.ToString, "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)

                    'xoa tieu de
                    If Not myDtF.Delete(Me.txtSoDt.EditValue.ToString.Trim) Then
                        XtraMessageBox.Show("Xóa tiêu đề dự toán không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End If

                    Return False
                    Exit Function

                End If
            End If
        Next

        Return True
    End Function

#End Region

    Private Sub frmDuToan_CapNhat_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Me.icbMaLoaiDt.Focus()
    End Sub

    Private Sub frmDuToan_CapNhat_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
        frmDuToan_CapNhat_Exists = False
    End Sub

    Private Sub frmDuToan_CapNhat_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitControl()
        InitComboBox_LoaiDt()
        InitLookup_DonVi()
        InitLookup_ThiCong()
        InitGrid()

        If frmDuToan_CapNhat_AddEdit = "add" Then
            AddNew()
        ElseIf frmDuToan_CapNhat_AddEdit = "edit" Then
            LoadEdit()
        End If

    End Sub

    Private Sub cmdThemDt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThemDt.Click
        If Me.btnThemDt.Text = "Thêm dự toán" Then 'add new
            AddNew()
            Me.icbMaLoaiDt.Focus()

        ElseIf Me.btnThemDt.Text = "Ghi dự toán" Then 'save

            If Me_AddEdit = "add" Then
                If Not CheckKey() Then Exit Sub
            End If

            If Not CheckInput() Then Exit Sub

            If Not SaveMaster() Then Exit Sub
            If Not SaveDetail() Then Exit Sub

            XtraMessageBox.Show("Ghi dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Status(0)
        End If

    End Sub

    Private Sub cmdSuaChietTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuaDt.Click
        If Me.btnSuaDt.Text = "Sửa" Then
            Status(2)
            Me.GridControl1.Focus()

        ElseIf Me.btnSuaDt.Text = "Bỏ qua" Then
            Status(0)

        End If
    End Sub

    Private Sub cmdThemVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdThemVatTu.Click
        AddDetail()
    End Sub

    Private Sub cmdXoaVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdXoaVatTu.Click
        Me.GridView1.DeleteSelectedRows()
    End Sub

    Private Sub cmdXoaChietTinh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoaDt.Click
        If XtraMessageBox.Show("Chắc chắn xóa dự toán?", "Quản lý chiết tính", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            Dim myDtF As New DuToanF
            If myDtF.Delete(Me.txtSoDt.EditValue.ToString.Trim) Then
                XtraMessageBox.Show("Xóa dữ liệu thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.Close()
            Else
                XtraMessageBox.Show("Xóa dữ liệu không thành công", "Quản lý chiết tính", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If

        End If
    End Sub

    Private Sub lueMaDv_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lueMaDv.EditValueChanged
        Dim myTcF As New ThiCongF
        Me_Dtb_SoTc = myTcF.GetMasterWhereMaDv(Me.lueMaDv.EditValue, Me.icbMaLoaiDt.EditValue, Me.txtSoDt.EditValue.ToString.Trim)
        Me.lueSoTc.Properties.DataSource = Me_Dtb_SoTc
        Me.lueSoTc.EditValue = ""
    End Sub

    Private Sub icbMaLoaiDt_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles icbMaLoaiDt.SelectedIndexChanged
        Dim myTcF As New ThiCongF
        Me_Dtb_SoTc = myTcF.GetMasterWhereMaDv(Me.lueMaDv.EditValue, Me.icbMaLoaiDt.EditValue, Me.txtSoDt.EditValue.ToString.Trim)
        Me.lueSoTc.Properties.DataSource = Me_Dtb_SoTc
        Me.lueSoTc.EditValue = ""

        If Me.lueSoTc.EditValue <> "" Then
            LoadGrid_SoTc(Me.lueSoTc.EditValue, Me.icbMaLoaiDt.EditValue)
        End If
    End Sub

    Private Sub lueSoTc_EditValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lueSoTc.EditValueChanged
        If Me.lueSoTc.EditValue <> "" Then
            LoadGrid_SoTc(Me.lueSoTc.EditValue, Me.icbMaLoaiDt.EditValue)
        End If
    End Sub

#Region "Focus"
    Private Sub icbMaLoaiDt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles icbMaLoaiDt.KeyDown
        If e.KeyCode = Keys.Enter Then Me.txtSoDt.Focus()
    End Sub

    Private Sub txtSoDt_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSoDt.KeyDown
        If e.KeyCode = Keys.Enter Then Me.datNgayLap.Focus()
    End Sub

    Private Sub datNgayLap_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles datNgayLap.KeyDown
        If e.KeyCode = Keys.Enter Then Me.lueMaDv.Focus()
    End Sub

    Private Sub lueMaDv_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lueMaDv.KeyDown
        If e.KeyCode = Keys.Enter Then Me.lueSoTc.Focus()
    End Sub

    Private Sub lueSoTc_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lueSoTc.KeyDown
        If e.KeyCode = Keys.Enter Then Me.GridControl1.Focus()
    End Sub

#End Region

End Class